import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common';
import { Request } from 'express';
import { appConfig } from './app.config';

@Injectable()
export class BasicGuard implements CanActivate {
    canActivate(context: ExecutionContext) {
        const request = context.switchToHttp().getRequest<Request>();
        const sysAuthorization = `Basic ${Buffer.from(
            `${appConfig.basicAuth.username}:${appConfig.basicAuth.password}`
        ).toString('base64')}`;
        let authorization = request.headers['authorization'];
        if (authorization !== sysAuthorization) {
            return false;
        }
        return true;
    }
}
